package com.example.zwh.computershop.home;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.particulars.particulars;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Notification.CATEGORY_PROMO;

public class GuidanceActivity extends AppCompatActivity {

    static int sleeps = 5;//这是倒计时的时间
    static final int TEXT = 1;
    static String TAG = "Shenyutian";
    @BindView(R.id.tv_goto_home)
    TextView tvGotoHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guidance);
        ButterKnife.bind(this);
        Notifi_ad();
//        test();
        Thread time_run = new Thread(new times_Thread());
        time_run.start();
}

    @OnClick(R.id.tv_goto_home)
    public void onViewClicked() {
        gotohome();
    }

    //计算时间线程
    class times_Thread extends Thread {
        @Override
        public void run() {
            while (sleeps >= 1) {
                try {
                    Message message = new Message();
                    message.what = TEXT;
                    handler.sendMessage(message);
                    sleeps--;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
//                Log.e(TAG, "sleep: " + sleeps);
            }
        }
    }

//    通知的广告栏
    private void Notifi_ad() {
        Intent intent = new Intent(this, particulars.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationManager manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, CATEGORY_PROMO)
                .setContentTitle("广告来了")
                .setContentText("广告来了")
                .setSubText("这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章这是一段长文章")
                .setWhen(System.currentTimeMillis())//通知创建的时间
                .setSmallIcon(R.mipmap.ic_launcher)//通知的小图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.xr_03))//下拉下拉列表里面的图标（大图标）
                .setContentIntent(pendingIntent)//通知处理---进行活动跳转
                .setAutoCancel(true)//点击之后关闭通知
                .setVibrate(new long[]{0,1000,1000,1000})//震动
                .setDefaults(NotificationCompat.PRIORITY_MAX)//使用默认的通知提示  现在设为最高
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.xr_03)))
                .build();
        manger.notify(1, notification);//显示通知


    }

    //跳到真正的主界面
    public void gotohome() {
        if(sleeps >= 0) {
            sleeps = -1;
            handler.getLooper();
        }
        Intent intent = new Intent(this, Home_Activity.class);
        startActivity(intent);
        finish();
    }

    //测试类
    private void test() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                try {

                    String uid = "2";//用户id
                    String pid = "P001";//商品id
                    String anum = "10";//商品数量

                    /***
                     * 下面方法要用那个，就用哪个其他注释掉
                     */
                    url = new URL("http://39.108.12.42:8080/anServer/dropCartServlet?uid="+uid+"&pid="+pid);//删除购物车
//                    url = new URL("http://39.108.12.42:8080/anServer/alterCartServlet?uid="+uid+"&pid="+pid+"&anum="+anum);//修改购物车
//                    url = new URL("http://39.108.12.42:8080/anServer/inCartServlet?uid="+uid+"&pid="+pid+"&anum="+anum);//加入购物车  不能增加现有的

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String str = reader.readLine();
//                    Log.d(TAG, "成功");
                    Log.d(TAG, "返回结果：(1失败，2成功)"+str);
                } catch (MalformedURLException e) {
                    Log.d(TAG, "错误1");
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    Log.d(TAG, "错误2");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d(TAG, "错误3");
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TEXT:
                    tvGotoHome.setText("跳过"+sleeps);
                    if(sleeps <= 0)
                        gotohome();
                    break;
                    default:
                        break;
            }
        }
    };

}
